package com.cloudptt.api.creator;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.StringWriter;

import org.xmlpull.v1.XmlSerializer;

import android.content.Context;
import android.util.Xml;

import com.cloudptt.api.pjsip.utils.MD5;
import com.mcptt.db.model.UserInfo;

/**
 * SIP消息body里xml的拼装
 * @author TP
 *
 */
public class SysMsgCreator {

	
	private String userKey = null;

	public SysMsgCreator(String userKey) {
		this.userKey = userKey;
	}

	/**
	 * 查询群组成员信息
	 * @param context
	 * @param info
	 * @param rdmnum
	 * @param checkSun
	 * @return
	 */
	public String queryGroupMembInfoMsg(Context context,UserInfo info,int rdmnum,String checkSun)
	{
		String groupId = info.getAddrId();
		XmlSerializer serializer = Xml.newSerializer();  
		StringWriter writer = new StringWriter();  
		try{  
			serializer.setOutput(writer);  

			// <?xml version=”1.0″ encoding=”UTF-8″ standalone=”yes”?>  
			serializer.startDocument("UTF-8",true);  

			// <blog number=”1″>  
			serializer.startTag("","pttrefreshgrpmbrreq");  

			// <title>Android XML</title>  
			serializer.startTag("","PUID");  
			serializer.text(userKey);  
			serializer.endTag("","PUID");  

			// <url>http://www.moandroid.com/?p=508 </url>  
			serializer.startTag("","GID");  
			serializer.text(groupId);  
			serializer.endTag("","GID");  

			serializer.startTag("","MbrCheckSum");  
			serializer.text(checkSun);  
			serializer.endTag("","MbrCheckSum");  

			serializer.startTag("","RandomNumber");  
			serializer.text(""+rdmnum);  
			serializer.endTag("","RandomNumber");  

			// </blog>  
			serializer.endTag("","pttrefreshgrpmbrreq");  
			serializer.endDocument();  
			return writer.toString();  
		}  
		catch(Exception e)  
		{  
			throw new RuntimeException(e);  
		}
	}

	/**
	 * 查询群组成员状态（呼叫激活时有效）
	 * @param groupId
	 * @param oprType
	 * @return
	 */
	public String queryGroupMembStatusMsg(String groupId,int oprType)
	{
		XmlSerializer serializer = Xml.newSerializer();  
		StringWriter writer = new StringWriter();  
		try{  
			serializer.setOutput(writer);  

			// <?xml version=”1.0″ encoding=”UTF-8″ standalone=”yes”?>  
			serializer.startDocument("UTF-8",true);  

			// <blog number=”1″>  
			serializer.startTag("","pttqrygrpmbrstatusreq");  

			// <title>Android XML</title>  
			serializer.startTag("","PUID");  
			serializer.text(userKey);  
			serializer.endTag("","PUID");  

			// <url>http://www.moandroid.com/?p=508 </url>  
			serializer.startTag("","GID");  
			serializer.text(groupId);  
			serializer.endTag("","GID");  

			serializer.startTag("","OprType");  
			serializer.text(""+ oprType);  
			serializer.endTag("","OprType");  

			// </blog>  
			serializer.endTag("","pttqrygrpmbrstatusreq");  
			serializer.endDocument();  
			return writer.toString();  
		}  
		catch(Exception e)  
		{  
			throw new RuntimeException(e);  
		}
	}

	/**
	 * 修改密码
	 * @param oldPassword
	 * @param newPassword
	 * @return
	 */
	public String modifyPasswordMsg(String oldPassword, String newPassword ) {
		XmlSerializer serializer = Xml.newSerializer();  
		StringWriter writer = new StringWriter();  
		String md5Password = "";
		try{  
			serializer.setOutput(writer);  

			serializer.startDocument("UTF-8",true);  

			serializer.startTag("","pttmodpasswdreq");  

			serializer.startTag("","PUID");  
			serializer.text(userKey);  
			serializer.endTag("","PUID");  

			serializer.startTag("","OldPassWord");  
			md5Password = MD5.calculateMD5(oldPassword);
			if(null==md5Password)
				md5Password = oldPassword;
			serializer.text(md5Password);  
			serializer.endTag("","OldPassWord");  

			serializer.startTag("","NewPassWord");  
			md5Password = MD5.calculateMD5(newPassword);
			if(null==md5Password)
				md5Password = newPassword;
			serializer.text(md5Password); 
			serializer.endTag("","NewPassWord");  

			serializer.endTag("","pttmodpasswdreq");  
			serializer.endDocument();  
			return writer.toString();  
		}  
		catch(Exception e)  
		{  
			throw new RuntimeException(e);  
		}
	}

	/**
	 * 查询一个用户
	 * @param num
	 * @return
	 */
	public String qryNewUserMsg(String num)
	{
		String req ="";
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		XmlSerializer serializer = Xml.newSerializer();
		try 
		{

			serializer.setOutput(baos, "UTF-8");  
			serializer.startDocument("UTF-8", true);
			serializer.startTag("", "pttqueryuserinforeq");			    
			serializer.startTag("", "Number");
			serializer.text(num);
			serializer.endTag("", "Number");
			serializer.endTag("", "pttqueryuserinforeq");
			serializer.endDocument();
			baos.flush();
			req = baos.toString();
			baos.close();

		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return req;
	}
}
